package cn.b.sky.admin.controller;

import cn.b.sky.admin.constants.SystemConstant;
import cn.b.sky.admin.model.BatchJobReport;
import cn.b.sky.admin.model.DWZResult;
import cn.b.sky.admin.model.FunctionGroup;
import cn.b.sky.admin.model.condition.FunctionGroupCondition;
import cn.b.sky.admin.service.FunctionGroupService;
import cn.b.sky.admin.utils.ManagementSecurity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * <strong>Title : FunctionGroupController.java<br>
 * </strong> <strong>Package : cn.b.sky.admin.controller<br>
 * </strong> <strong>Description : </strong>功能组操作控制器<br>
 * <strong>Create on : 2016-6-7 上午11:36:02<br>
 * </strong>
 * <p>
 * 
 * @author JunLin.Yang<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/management/functionGroup/*")
public class FunctionGroupController extends BaseController {

	@Autowired
	FunctionGroupService functionGroupService;

	/**
	 * 
	 * <strong>Title : listFunctionGroupsPageByCondition<br></strong>
	 * <strong>Description : </strong>根据查询条件查询功能组分页信息<br> 
	 * <strong>Create on : 2016-6-7 下午5:05:46<br></strong>
	 * <p>
	 * @param navTabId 分页TAB的ID
	 * @param condition 查询条件
	 * @param model 值对象
	 * @return
	 * String 页面地址
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping(value = "listFunctionGroupsPageByCondition")
	public String listFunctionGroupsPageByCondition(String navTabId,
			@ModelAttribute("condition") FunctionGroupCondition condition,
			Model model) {

		model.addAttribute(NAVTABID, navTabId);

		model.addAttribute("page", this.functionGroupService
				.selectFunctionGroupsPageByCondition(condition));

		return "admin/functiongroup/functionGroup_list";
	}

	/**
	 * 
	 * <strong>Title : viewFunctionGroup<br></strong>
	 * <strong>Description : </strong>查看功能组信息<br> 
	 * <strong>Create on : 2016-6-7 下午5:06:28<br></strong>
	 * <p>
	 * @param navTabId 分页TAB的ID
	 * @param functionGroupCode 功能组编号
	 * @param model 值对象
	 * @return
	 * String 跳转页面地址
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping(value = "viewFunctionGroup")
	public String viewFunctionGroup(String navTabId, String functionGroupCode,
			Model model) {

		model.addAttribute(NAVTABID, navTabId);

		model.addAttribute("functionGroup", this.functionGroupService
				.getFunctionGroupByPK(functionGroupCode));

		return "admin/functiongroup/functionGroup_view";
	}

	/**
	 * 
	 * <strong>Title : toEditFunctionGroup<br></strong>
	 * <strong>Description : </strong>功能组信息修改前置操作<br> 
	 * <strong>Create on : 2016-6-7 下午5:07:08<br></strong>
	 * <p>
	 * @param navTabId 分页TAB的ID
	 * @param functionGroupCode 功能组编号
	 * @param model 值对象
	 * @return
	 * String 页面跳转地址
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping(value = "toEditFunctionGroup")
	public String toEditFunctionGroup(String navTabId,
			String functionGroupCode, Model model) {

		model.addAttribute(NAVTABID, navTabId);

		model.addAttribute("functionGroup", this.functionGroupService
				.getFunctionGroupByPK(functionGroupCode));

		return "admin/functiongroup/functionGroup_edit";
	}

	/**
	 * 
	 * <strong>Title : toAddFunctionGroup<br></strong>
	 * <strong>Description : </strong>功能组信息新增前置操作<br> 
	 * <strong>Create on : 2016-6-7 下午5:07:43<br></strong>
	 * <p>
	 * @param navTabId 分页TAB的ID
	 * @param model 值对象
	 * @return
	 * String 页面跳转地址
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping(value = "toAddFunctionGroup")
	public String toAddFunctionGroup(String navTabId,Model model) {

		model.addAttribute(NAVTABID, navTabId);
		return "admin/functiongroup/functionGroup_edit";
	}

	/**
	 * 
	 * <strong>Title : saveFunctionGroup<br></strong>
	 * <strong>Description : </strong>功能组信息保存或更新<br> 
	 * <strong>Create on : 2016-6-7 下午5:08:25<br></strong>
	 * <p>
	 * @param navTabId 分页TAB的ID
	 * @param functionGroup 功能组信息对象
	 * @param model 值对象
	 * @return
	 * DWZResult 执行结果
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping(value = "saveFunctionGroup")
	@ResponseBody
	public DWZResult saveFunctionGroup(String navTabId,
			FunctionGroup functionGroup, Model model) {
		if (null == functionGroup) {
			return ajaxError(SystemConstant.PARAMETERNULL);
		}

		setCreateInfo(functionGroup);
		functionGroupService.insertOrUpdateFunctionGroup(functionGroup);

		return dialogAjaxDone(SystemConstant.DWZ_CODE_OK_MESSAGE, navTabId,
				null, null, null);
	}

	/**
	 * 
	 * <strong>Title : deleteFunctionGroupBatchByPK<br></strong>
	 * <strong>Description : </strong>根据主键批量删除功能组信息<br> 
	 * <strong>Create on : 2016-6-7 下午5:08:57<br></strong>
	 * <p>
	 * @param model 值对象
	 * @param navTabId 分页TAB的ID
	 * @param request HTTP请求
	 * @return
	 * DWZResult 执行结果
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping("deleteFunctionGroupBatchByPK")
	@ResponseBody
	public DWZResult deleteFunctionGroupBatchByPK(Model model, String navTabId,
												  HttpServletRequest request) {
		String functionGroupCodes = request.getParameter("ids");
		if (StringUtils.isEmpty(functionGroupCodes)) {
			return ajaxError(SystemConstant.PARAMETERNULL);
		}
		String[] functionGroupCodeArray = functionGroupCodes.split(",");
		List<BatchJobReport> reports = new ArrayList<BatchJobReport>(
				functionGroupCodeArray.length);
		boolean hasFail = false;
		BatchJobReport report = null;
		for (String functionGroupCode : functionGroupCodeArray) {
			report = this.functionGroupService
					.deleteFunctionGroupByPK(functionGroupCode);
			reports.add(report);
			if (!report.isSuccess()) {
				hasFail = true;
			}
		}
		if (!hasFail) {
			// 直接返回成功消息
			return navTabAjaxDone(navTabId, null, null, null,
					SystemConstant.DWZ_CODE_OK_MESSAGE, null);
		} else {
			return batchjobAjaxDone(request, reports);
		}

	}

	/**
	 * 
	 * <strong>Title : validatefunctionGroupCode<br></strong>
	 * <strong>Description : </strong>验证功能组编号是否存在<br> 
	 * <strong>Create on : 2016-6-7 下午5:09:40<br></strong>
	 * <p>
	 * @param functionGroupCode 功能组编号
	 * @param model 值对象
	 * @return
	 * boolean 是否存在
	 * @throws 
	 * @author JunLin.Yang<br>
	 * @version <strong>v1.0.0</strong><br>
	 * <br>
	 * <strong>修改历史:</strong><br>
	 * 修改人 | 修改日期 | 修改描述<br>
	 * -------------------------------------------<br>
	 * <br>
	 * <br>
	 */
	@RequestMapping("validatefunctionGroupCode")
	@ResponseBody
	public boolean validatefunctionGroupCode(String functionGroupCode,
			Model model) {
		if (StringUtils.isNotBlank(functionGroupCode) 
				&& null!=this.functionGroupService.getFunctionGroupByPK(functionGroupCode.toUpperCase())) {
			return false;
		}
		
		return true;
	}

	private void setCreateInfo(FunctionGroup functionGroup) {
		functionGroup.setCreateUser(ManagementSecurity.getCurrentUserCode());
		functionGroup.setCreateDateTime(new Date());
		functionGroup.setModifyUser(ManagementSecurity.getCurrentUserCode());
		functionGroup.setModifyDateTime(new Date());
	}

}
